package com.learn.algorlthm;

/**
 * @classname:
 * @Description:链表中倒数第K个结点
 * @author: yfpu(fengpu.yang @ ucarinc.com)
 * @Date: 2018-07-24 16:07
 *
 * 题目描述
 * 输入一个链表，输出该链表中倒数第k个结点。
 */
public class Learn22 {


    public static ListNode findKthToTail(ListNode head, int k) {
        if (head == null)
            return null;
        ListNode p1 = head;
        while (p1 != null && k-- >0)
            p1 = p1.next;
        if (k > 0)
            return null;

        ListNode p2 = head;
        while (p1 != null) {
            p1 = p1.next;
            p2 = p2.next;
        }

        return p2;
    }


    class ListNode {
        public String val;
        public ListNode next;
    }

}
